import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/widgets/search_layout/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/widgets/search_layout/search_notifier.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/widgets/filter_layout/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class DeviceHeader extends StatefulWidget {
  final String? title;
  final Widget? leading;
  final bool showSearch;
  final bool showFloatBtn;
  final VoidCallback? floatOnPress;
  final Function(String? value)? onSearch;
  final List<Widget> children;
  final List<Widget>? actions;
  final List<Widget>? floatChildren;

  const DeviceHeader({
    super.key,
    this.title,
    this.leading,
    this.showSearch = true,
    this.showFloatBtn = true,
    this.floatOnPress,
    this.onSearch,
    required this.children,
    this.actions, this.floatChildren,
  });

  @override
  State<StatefulWidget> createState() => _DeviceHeaderState();

}

class _DeviceHeaderState extends State<DeviceHeader> {
  final SearchNotifier _controller = SearchNotifier();

  final Map _filter = {"pageNum": 1};
  final List _lockStatus = [{"name": "全部"}, {"name": "未关联房间"}];


  String getTitle(data) {
    return Utils.isNotEmpty(data) ? data : "";
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text(getTitle(widget.title), style: const TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimary,
        leading: widget.leading,
        actions: [
          if (widget.showSearch) IconButton(
            icon: const Icon(Icons.search),
            tooltip: "搜索",
            onPressed: () {
              _controller.isShow ? _controller.hide() : _controller.show();
            },
          ),
          if (widget.actions != null) ...?widget.actions,

        ],
      ),
      body: Stack(
        children: [
          ...widget.children,
          if (widget.showFloatBtn) Positioned(
            right: 30,
            bottom: 60,
            child: Column(
              children: [
                FloatingActionButton(
                  shape: const CircleBorder(),
                  foregroundColor: Colors.white,
                  backgroundColor: ThemeVars.colorPrimaryDark,
                  onPressed: widget.floatOnPress,
                  child: const Icon(Icons.add, size: 32,),
                ),
                if (widget.floatChildren != null) ...?widget.floatChildren,
              ]
            )
          ),
          if (widget.showSearch) SearchLayout(
            controller: _controller,
            onSearch: widget.onSearch,
          ),
        ],
      ),
    );
  }

}